package com.shuorikeji.huohuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.imageview.ShapeableImageView;
import com.shuorikeji.huohuo.bean.BookDetailInfoBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.utils.BusinessUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class TjBookAdapter extends RecyclerView.Adapter<TjBookAdapter.BaseViewHolder> {
    private final Context mContext;
    private List<BookDetailInfoBean> mData = new ArrayList<>();
    private ClickCallBack callBack;

    public TjBookAdapter(Context context, ClickCallBack callBackFlag) {
        this.mContext = context;
        this.callBack = callBackFlag;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tj_book_item_layout, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        BusinessUtils.setLoadImg(mContext, Constant.IMG_OSS_BASE_URL + mData.get(position).getCover_image(), holder.sivFm);
        if (position == 0) {
            holder.ivPmBg.setImageResource(R.mipmap.icon_home_xs_pm_1);
        } else if (position == 1) {
            holder.ivPmBg.setImageResource(R.mipmap.icon_home_xs_pm_2);
        } else if (position == 2) {
            holder.ivPmBg.setImageResource(R.mipmap.icon_home_xs_pm_3);
        } else {
            holder.ivPmBg.setImageResource(R.mipmap.icon_home_xs_pm_other);
        }
        holder.tvPm.setText(String.valueOf(position + 1));
        holder.tvName.setText(mData.get(position).getName());
        holder.tvTag.setText(mData.get(position).getAuthor());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onClick(1, mData.get(position));
                }
            }
        });
    }

    public void setData(List<BookDetailInfoBean> data) {
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView tvPm;
        TextView tvName;
        TextView tvTag;
        ShapeableImageView sivFm;
        ImageView ivPmBg;

        public BaseViewHolder(View itemView) {
            super(itemView);
            sivFm = itemView.findViewById(R.id.siv_fm);
            ivPmBg = itemView.findViewById(R.id.iv_pm_bg);
            tvPm = itemView.findViewById(R.id.tv_pm);
            tvName = itemView.findViewById(R.id.tv_name);
            tvTag = itemView.findViewById(R.id.tv_tag);
        }
    }
}
